/**
 *
 * jcrt and its source-code is licensed under the LGPL.
 * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
 * When copying, adapting, or redistributing this document in keeping with the guidelines above,
 * you are required to provide proper attribution to obinary.
 * If you reproduce or distribute the document without making any substantive modifications to its content,
 * please use the following attribution line:
 *
 * Copyright 2007 Atjava.com Ltd. (http://www.atjava.com) All rights reserved.
 *
 */
package com.atjava.jcrweb.servlet;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.atjava.jcrweb.JcrWebException;
import com.atjava.jcrweb.info.ContentInfo;
import com.atjava.jcrweb.info.DirectoryInfo;
import com.atjava.jcrweb.manager.ContentManager;
import com.atjava.jcrweb.manager.DataHelper;
import com.atjava.jcrweb.manager.DirectoryManager;
import com.atjava.jcrweb.manager.PackingToFlexHelper;
import com.atjava.jcrweb.manager.UserManager;
/**
 * 
 * 
 * @author biggie
 * @version $Revision: 1.11 $ $Date: 2008/07/17 02:07:39 $
 */
public class LoadMainDataFlexServlet extends FlexBaseServlet {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public String process(HttpServletRequest request,
			HttpServletResponse response, Map<String, String[]> inputdata)
			throws IOException, ServletException , JcrWebException , Exception {

		String type= getData(inputdata, "type");
		if(type.equals("loaddata")){
			return loadData(request,response,inputdata);
		}
		return null;
	}
	private String loadData(HttpServletRequest request,
			HttpServletResponse response, Map<String, String[]> inputdata)
	throws IOException, ServletException , JcrWebException , Exception {
		
		DirectoryManager dirManager = DirectoryManager.getInstance();
		ContentManager cntManager = ContentManager.getInstance();
		UserManager userManager = UserManager.getInstance();

		List dirlist = dirManager.getChildren(getData(inputdata,"path"));
		dirlist = DataHelper.permissionDirpathFilter(userManager.getLoginUserinfo(request.getSession()), dirlist);
		
		List conlist = cntManager.getChildren(getData(inputdata,"path"));
		conlist = DataHelper.permissionDirpathFilter(userManager.getLoginUserinfo(request.getSession()), conlist);

		String pathname = dirManager.getPathName(getData(inputdata,"path"));
		return toXmldata(dirlist,conlist,pathname);
	}
	private String toXmldata(List dirlist,List conlist,String pathname){
		PackingToFlexHelper helper = new PackingToFlexHelper();
		StringBuffer sb = new StringBuffer();
		sb.append("<pathname>"+pathname+"</pathname>");
		if((dirlist != null && dirlist.size() > 0)||(conlist != null && conlist.size() > 0)){
			int dirSize=0; int conSize = 0; int i=0;
			if(dirlist != null && dirlist.size() > 0){
				dirSize = dirlist.size();
				while(i<dirSize){
					DirectoryInfo dirInfo = (DirectoryInfo)dirlist.get(i);
					sb.append("<"+(i+1)+">");
					sb.append("<data>");
					sb.append(dirInfo.toXmlString());
					sb.append("<path>"+dirInfo.getPath()+"</path>");
					sb.append("<isfile>false</isfile>");
					sb.append("<picpath>images/folder.gif</picpath>");			
					sb.append("</data>");
					sb.append("</"+(i+1)+">");
					i++;
				}
			}
			if(conlist != null && conlist.size() > 0){
				conSize = conlist.size();
				for(int j=0;j<conlist.size();j++){
					ContentInfo conInfo = (ContentInfo)conlist.get(j);
					sb.append("<"+(i+1)+">");
					sb.append("<data>");
					sb.append(conInfo.toXmlString());
					sb.append("<path>"+conInfo.getPath()+"</path>");
					sb.append("<isfile>true</isfile>");
					sb.append("<picpath>images/file.gif</picpath>");				
					sb.append("</data>");
					sb.append("</"+(i+1)+">");
					i++;
				}
			}
			helper.setResultSize(String.valueOf((dirSize+conSize)));
		}else{
			helper.setIsError(false);
			helper.setResultData(sb.toString());
			return helper.toString();
		}
		helper.setIsError(false);
		helper.setResultData(sb.toString());
		return helper.toString();
	}
}
